<template>
  <div>
    <h1>{{id?"编辑":"新增"}}账号</h1>

    <el-form ref="form" label-width="80px">
      <el-form-item label-width="200px" label="账号">
        <el-input v-model="model.username"></el-input>
      </el-form-item>
      <el-form-item label-width="200px" label="密码">
        <el-input v-model="model.password"></el-input>
      </el-form-item>
      <el-form-item label-width="200px" label="用户名">
        <el-input v-model="model.name"></el-input>
      </el-form-item>
      <el-form-item label-width="200px" label="权限">
        <el-select v-model="model.power" placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label-width="200px">
        <el-button type="primary" @click="onSubmit">{{id?"立即修改":"立即创建"}}</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  props: {
    id: {}
  },
  data() {
    return {
      model: {},
      options: [
        {
          value: "admin",
          label: "超级管理员"
        },
        {
          value: "user",
          label: "用户"
        },
        {
          value: "no",
          label: "禁用"
        }
      ]
    };
  },
  created() {
    this.id && this.findIdModel();
    console.log(this.$http.defaults.baseURL, "");
  },
  methods: {
    load() {
      this.count += 2;
    },
    handleAvatarSuccess(res, file) {
      console.log(res, file);
      // this.model.imageUrl = URL.createObjectURL(file.raw);
      this.$set(this.model, "img", res.url);
    },
    async onSubmit() {
      if (this.id) {
        await this.$http.put(`/rest/User/${this.id}`, this.model);
      } else {
        await this.$http.post("/rest/User", this.model);
      }
      this.$router.push("/User/UserList");
      this.$message({
        message: `操作成功`,
        type: "success"
      });
    },
    async findIdModel() {
      const mode = await this.$http.get(`/rest/User/${this.id}`);
      console.log(mode);
      this.model = mode.data;
    }
  }
};
</script>

<style>
.infinite-list {
  height: 200px;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px !important;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>